package cloud.maque.base.dingding.impl;

import cloud.maque.base.dingding.conf.DingDingClientConfig;
import cloud.maque.base.dingding.service.DingdingTokenService;
import cloud.maque.framework.conf.PlatformConfigProperties;
import cn.hutool.core.util.StrUtil;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.aliyun.tea.TeaException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhuyf
 * @date 2023/3/3 11:09
 */
@Service
public class DingdingTokenServiceImpl implements DingdingTokenService {


    @Autowired
    PlatformConfigProperties platformConfigProperties;

    /**
     * 获取企业内网 AccessToken
     *
     * @return
     */
    @Override
    public String GetAccessEnterpriseToken() {

        return GetAccessEnterpriseToken(platformConfigProperties.getDingding().getAppKey(), platformConfigProperties.getDingding().getAppSecret());

    }

    @Override
    public String GetAccessEnterpriseToken(String clientId, String clientSecret) {
        String accessToken = "";
        com.aliyun.dingtalkoauth2_1_0.Client client = null;
        try {
            client = DingDingClientConfig.authClient();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest().setAppKey(clientId).setAppSecret(clientSecret);

        GetAccessTokenResponse response = null;
        try {
            response = client.getAccessToken(getAccessTokenRequest);
            accessToken = response.getBody().getAccessToken();

        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                throw new RuntimeException(err.message);
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                throw new RuntimeException(err.message);
            }
        }

        if (StrUtil.isEmptyIfStr(accessToken)) {
            throw new RuntimeException("生成Token为空");
        }

        return accessToken;
    }

    @Override
    public String GetAccessUserToken(String authCode) {

        return GetAccessUserToken(platformConfigProperties.getDingding().getAppKey(), platformConfigProperties.getDingding().getAppSecret(), authCode);

    }

    @Override
    public String GetAccessUserToken(String clientId, String clientSecret, String authCode) {
        com.aliyun.dingtalkoauth2_1_0.Client client = null;
        try {
            client = DingDingClientConfig.authClient();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
                //应用基础信息-应用信息的AppKey,请务必替换为开发的应用AppKey
                .setClientId(clientId)
                //应用基础信息-应用信息的AppSecret，,请务必替换为开发的应用AppSecret
                .setClientSecret(clientSecret).setCode(authCode).setGrantType("authorization_code");

        GetUserTokenResponse getUserTokenResponse = null;
        try {
            getUserTokenResponse = client.getUserToken(getUserTokenRequest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        //获取用户个人token
        String accessToken = getUserTokenResponse.getBody().getAccessToken();

        if (StrUtil.isEmptyIfStr(accessToken)) {
            throw new RuntimeException("生成Token为空");
        }
        return accessToken;
    }
}
